using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;



namespace WinCANTool
{
    
    
    
    partial class FlashWindow1319
    {
            
        public Netparms Net = new Netparms();
        delegate void AddString(object sender, NodeFindArgs e);
        delegate void InhibitDelegate(object sender, EventArgs e);
        NewMessage InhibitMessage = new NewMessage();
        Thread TxInhibitThread;
        public bool InhibitThreadOn = false;
        public bool HaltThreadRead = false;

        public void GetNodeInfo()
        {
            GetNodeHandle();
            HaltThreadRead = true;
            //Net.CurrentBaudRate = 2;
            Net.CurrentBaudRate = ParentForm.GetBaudrate();
            NodeFind NodeFinder = new NodeFind(Net);
            NodeFinder.NodeFinderEvent += new NodeFindHandler(NodeFindReady);
            Thread ThreadNodeFind = new Thread(new ThreadStart(NodeFinder.FindNodes));

            ThreadNodeFind.Start();


        }

        private void GetNodeHandle()
        {
            Net.ClientHndl = ParentForm.GetClientHandle();
            Net.NetHndl = ParentForm.GetNetHandle();
            Net.HwHndl = ParentForm.GetHwHandle();
            Net.BaudRateValues = ParentForm.GetBaudValues();
            Net.BaudRateStrings = ParentForm.GetBaudStrings();
        }

        private void FillMessage(ref NewMessage InhibMess)
        {
            InhibMess.SaveID_DLC_Int(0x7E5, 8);
            InhibMess.SaveSendRate(40);
            InhibMess.SaveDataInt(0, 0x40);
            InhibMess.SaveDataInt(1, 0x49);
            InhibMess.SaveDataInt(2, 0x43);
        }


        public void StartInhibit()
        {
            GetNodeHandle();
            FillMessage(ref InhibitMessage);
            InhibitThreadOn = true;
            CycleThread InhibitCycleThread = new CycleThread(InhibitMessage, Net, 1000);
            TxInhibitThread = new Thread(new ThreadStart(InhibitCycleThread.TxThread));
            InhibitCycleThread.TxReady += new NewMessageHandler(NextInhibitDone);
            TxInhibitThread.Start();
            //NewThread.TxReady += new NewMessageHandler(AddTxMessageEvent1);
            //
            //tx_thread = new Thread(new ThreadStart(NewThread.TxThread));
            //tx_thread.Start();

        }

        public void StopInhibit()
        {
            if (InhibitThreadOn)
            {
                TxInhibitThread.Abort();
                InhibitThreadOn = false;
            }
        }
        
        
        
        
        
        
        private void NodeFindReady(object sender, NodeFindArgs e)
        {
            if (this.FlashtextBox.InvokeRequired)
            {
                AddString d = new AddString(NodeFindReady);
                this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                if (e.ReleaseThreadHalt)
                {
                    ParentForm.newMDIChild1.HaltThreadRead = false;
                    //ParentForm.DisplayBaudRate(e.BaudParam);
                }
                else
                {
                    FlashtextBox.Text += e.NodeFindString;
                    ParentForm.NodeFindResults += e.NodeDisplayString;
                    //progressBar1.Value = e.Progress;
                }
            }
        }


        private void NextInhibitDone(object sender, EventArgs e)
        {

            if (InhibitProgressBar.InvokeRequired)
            {
                InhibitDelegate d = new InhibitDelegate(NextInhibitDone);
                this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                if (InhibitProgressBar.Value < 100)
                    InhibitProgressBar.Value++;
                else
                    InhibitProgressBar.Value = 0;

            }

        }


       

    }

}
